package com.sc.common.util;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class IPUtils {
    private static final Logger LOG = LoggerFactory.getLogger(IPUtils.class);
    private static String LOCAL_IP = "";
    public IPUtils() {
    }
    public static String getLocalIp() {
        if (StringUtils.isBlank(LOCAL_IP)) {
            try {
                Enumeration ni = NetworkInterface.getNetworkInterfaces();

                while(ni.hasMoreElements()) {
                    NetworkInterface eth = (NetworkInterface)ni.nextElement();
                    Enumeration add = eth.getInetAddresses();

                    while(add.hasMoreElements()) {
                        InetAddress i = (InetAddress)add.nextElement();
                        if (i instanceof Inet4Address && i.isSiteLocalAddress()) {
                            LOCAL_IP = i.getHostAddress();
                            return LOCAL_IP;
                        }
                    }
                }
            } catch (SocketException var4) {
                LOG.error("get local ip failure", var4);
            }
        }
        return LOCAL_IP;
    }
    public static String parseIceConIP(String iceConInfo) {
        return iceConInfo.replaceAll(":", "_").replace("local address =", "localIP:").replace("remote address =", "clientIP:").replace("\n", ",");
    }

    public static String[] parseIceConfToIPArr(String iceConInfo) {
        return iceConInfo.replaceAll(":", "_").replace("local address =", "").replace("remote address =", "").replace("\n", ",").split(",");
    }
}
